package com.jphoebe.plugin.mybatis.intention.generate.mapper;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jphoebe.plugin.mybatis.bean.setting.GlobalSetting;
import com.jphoebe.plugin.mybatis.form.CreateMybatisXmlFileForm;
import com.jphoebe.plugin.mybatis.intention.GenericIntention;
import com.jphoebe.plugin.mybatis.service.setting.GlobalSettingService;
import com.jphoebe.plugin.mybatis.utils.Il8nUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author yanglin
 * @update JPhoebe
 * @function 生成mapper xml
 */
public class GenerateMapperIntention extends GenericIntention {

    public GenerateMapperIntention() {
        super(GenerateMapperChooser.INSTANCE);
    }

    @NotNull
    @Override
    public String getText() {
        GlobalSetting globalSetting = GlobalSettingService.getInstance().getState();
        return Il8nUtil.getCurrnetSysProp(globalSetting.getLanguage()).getStr("java.createMybatisXmlFile");
    }

    @Override
    public boolean startInWriteAction() {
        return false;
    }

    @Override
    public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
        PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
        PsiClass clazz = PsiTreeUtil.getParentOfType(element, PsiClass.class);
        // 创建mybatis xml弹出框
        CreateMybatisXmlFileForm createMybatisXmlFileForm = new CreateMybatisXmlFileForm();
        createMybatisXmlFileForm.setEditor(editor);
        createMybatisXmlFileForm.setPsiClass(clazz);
        createMybatisXmlFileForm.pack();
        createMybatisXmlFileForm.setVisible(true);
    }

}